package com.honghu.villagepost.activity

import android.content.Context
import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.honghu.villagepost.R
import com.honghu.villagepost.adapter.GoodsAdapter
import com.honghu.villagepost.api.ApiService
import com.honghu.villagepost.base.BaseListActivity
import com.honghu.villagepost.callback.DialogCallback
import com.honghu.villagepost.callback.LzyResponse
import com.honghu.villagepost.constant.Constants
import com.honghu.villagepost.entity.Goods
import com.honghu.villagepost.util.DensityUtil
import com.honghu.villagepost.widget.decoration.GridSpacingItemDecoration
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_search.*
import kotlinx.android.synthetic.main.include_refresh_layout.*
import kotlinx.android.synthetic.main.include_refresh_layout.recyclerView

/**
 * @author: mqwang
 * @create: 3/16/21 10:12 AM
 * @Description:搜索页面
 */
class SearchActivity : BaseListActivity<Goods>() {

    private val goodsAdapter : GoodsAdapter by lazy {
        GoodsAdapter()
    }

    companion object{
        fun self(context: Context){
            val intent = Intent(context, SearchActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun isNeedTitleBar(): Boolean {
        return false
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_search
    }

    override fun getEnableRefresh(): Boolean {
        return false
    }

    override fun getEnableAutoRefresh(): Boolean {
        return false
    }

    override fun bindRecyclerView(recLv: RecyclerView?) {
        btn_back.setOnClickListener {
            onBackPressed()
        }

        et_search.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                onRefresh(refreshLayout)
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

        })
        et_search.setOnEditorActionListener(TextView.OnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                onRefresh(refreshLayout)
                return@OnEditorActionListener true
            }
            false
        })

        recLv!!.layoutManager = GridLayoutManager(this, 2)
        recLv.addItemDecoration(GridSpacingItemDecoration(2, DensityUtil.dp2px(8f), true))
        recyclerView.adapter = goodsAdapter

        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            var goods = adapter.getItem(position) as Goods
            GoodsDetailsActivity.self(mContext, goods.id)
        }
    }

    override fun loadData(page: Int, pageCount: Int) {
        val searchKey = et_search.text.toString()
        if (TextUtils.isEmpty(searchKey)){
            goodsAdapter.data.clear()
            goodsAdapter.notifyDataSetChanged()
        }else{
            ApiService.goods(this, 0, 0, searchKey, page, object : DialogCallback<LzyResponse<Goods>>(){
                override fun onSuccess(response: Response<LzyResponse<Goods>>?) {
                    onGetDataSuc(response!!.body().data.records)
                }
            })
        }
    }

    override fun getData(): MutableList<Goods> {
        return goodsAdapter.data
    }

    override fun bindData(isRefresh: Boolean, list: MutableList<Goods>?) {
        if (isRefresh){
            goodsAdapter.setNewData(list)
        }else{
            goodsAdapter.addData(list!!)
        }
    }

}